﻿using System;
using System.Collections.Generic;
using System.Linq;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace SysManage.Module.ScoreManage
{
    public partial class PartyObjectScoreMemberList : PageBase
    {
        #region 属性

       

        #endregion

        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        /*public override string ViewPower
        {
            get
            {
                return "PartyObjectScoreView";
            }
        }*/
        #endregion

        public string ObjectType
        {
            get
            {
                return this.Request["ObjectType"] ?? "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.CurrentUser.LoginID == "admin")
            {
                if (ObjectType == "")
                {
                    bianji.Hidden = true;
                    state.Hidden = true;
                    defen.Hidden = false;
                    beizhu.Hidden = false;
                    beizhu1.Hidden = true;
                    yijian.Hidden = true;
                    ddlCategory.Hidden = false;
                }
                else
                {
                    ddlCategory.Hidden = true;
                    bianji.Hidden = false;
                    state.Hidden = false;
                    defen.Hidden = true;
                    beizhu.Hidden = true;
                    beizhu1.Hidden = false;
                    yijian.Hidden = false;
                }
                shanchu.Hidden = true;
            }
            else {
                string ssss = this.CurrentUser.IsPartyWorker.ToString();
                if (ssss == "True")
                {
                    if (ObjectType == "")
                    {
                        bianji.Hidden = true;
                        state.Hidden = true;
                        defen.Hidden = false;
                        beizhu.Hidden = false;
                        beizhu1.Hidden = true;
                        yijian.Hidden = true;
                        ddlCategory.Hidden = false;
                    }
                    else
                    {
                        ddlCategory.Hidden = true;
                        bianji.Hidden = false;
                        state.Hidden = false;
                        defen.Hidden = true;
                        beizhu.Hidden = true;
                        beizhu1.Hidden = false;
                        yijian.Hidden = false;
                    }
                }
                else {
                    bianji.Hidden = true;
                    if (ObjectType == "")
                    {
                        ddlCategory.Hidden = false;
                        state.Hidden = true;
                        defen.Hidden = false;
                        beizhu.Hidden = false;
                        beizhu1.Hidden = true;
                        yijian.Hidden = true;
                    }
                    else
                    {
                        state.Hidden = false;
                        defen.Hidden = true;
                        beizhu.Hidden = true;
                        beizhu1.Hidden = false;
                        yijian.Hidden = false;
                        ddlCategory.Hidden = true;
                    }
                }
                shanchu.Hidden = true;
            }

            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        private void BindData()
        {

            // 权限检查
            //CheckPowerWithButton("PartyObjectScoreAdd", btnAdd);

            btnAdd.OnClientClick = Dialog.GetShowReference("PartyObjectScoreEdit.aspx?ObjectType=member");
            ControlBusiness.ControlBind(ddlCategory, DB.PartyObjectScoreRules.Where(a => a.ObjectType == "Member"), "Name", "Category", true);

            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();

        }

        
        private void LoadData()
        {
            if (ObjectType == "")
            {
                var ds = PartyObjectScore.GetGridDataMember(Grid1.PageIndex, Grid1.PageSize, ddlCategory.SelectedValue, txtName.Text,CurrentUser.LoginID);
                Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                Grid1.DataSource = ds.Tables[0];
                Grid1.DataBind();
            }
            else {
                string ssssss = CurrentUser.IsPartyWorker.ToString();
                var current1 = DB.PartyOrgBranchs.Find(this.CurrentUser.BranchID);
                string bm = current1.BranchMark;
                if (ssssss=="True")
                {
                    var ds = PartyObjectScoreApply.GetGridDataMember(Grid1.PageIndex, Grid1.PageSize, "MemberApplyToBranch", txtName.Text, CurrentUser.BranchID, CurrentUser.LoginID,bm);
                    Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                    Grid1.DataSource = ds.Tables[0];
                    Grid1.DataBind();
                }
                else {
                    var ds = PartyObjectScoreApply.GetGridDataMember1(Grid1.PageIndex, Grid1.PageSize, "MemberApplyToBranch", txtName.Text, CurrentUser.LoginID);
                    Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                    Grid1.DataSource = ds.Tables[0];
                    Grid1.DataBind();
                }
            }
            
        }



        #endregion
        #region 事件


        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("PartyObjectScoreEdit", Grid1, "editField");
            CheckPowerWithLinkButtonField("PartyObjectScoreDelete", Grid1, "deleteField");
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            LoadData();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string PartyObjectScoreId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("PartyObjectScoreDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                var del = DB.PartyObjectScores.FirstOrDefault(t => t.ID == PartyObjectScoreId);
                DB.PartyObjectScores.Remove(del);
                DB.SaveChanges();
                LoadData();
            }
        }


        #endregion

        #region 按钮事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("PartyObjectScoreDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyObjectScores.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyObjectScores.Remove(u));
            DB.SaveChanges();

            // 重新绑定表格
            LoadData();
        }
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            LoadData();
        }


        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            LoadData();
        }
    }


}